/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";

import defaultAvatar from "@/assets/images/defaultAvatar.jpg";
import { ElTag, ElImage } from "element-plus";
import { useMessage } from "@/hooks/useMessage.ts";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { deleteByIds } from "../../../api/attractionUser.ts";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any
): MaProTableColumns[] {
  const msg = useMessage();

  return [
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
    },
    {
      label: "用户头像",
      prop: "avatar",
      cellRender: ({ row }) => {
        if (!row.avatar) {
          return h("span", { style: { color: "#999" } }, "暂无图片");
        }

        const env = import.meta.env;
        const baseUrl =
          env.VITE_OPEN_PROXY === "true"
            ? env.VITE_PROXY_PREFIX
            : env.VITE_APP_API_BASEURL;

        let imageUrl = row.avatar;
        if (!row.avatar.startsWith("http")) {
          imageUrl = `${baseUrl}${row.avatar}`;
        }

        return h(
          ElImage,
          {
            src: imageUrl,
            alt: "商品图片",
            style: {
              width: "60px",
              height: "40px",
              borderRadius: "4px",
              border: "1px solid #ddd",
            },
            fit: "cover",
            previewSrcList: [imageUrl],
            previewTeleported: true,
            onError: () => {
              console.warn("商品图片加载失败:", imageUrl);
            },
          },
          {
            error: () =>
              h(
                "div",
                {
                  style: {
                    width: "60px",
                    height: "40px",
                    display: "flex",
                    alignItems: "center",
                    justifyContent: "center",
                    backgroundColor: "#f5f5f5",
                    border: "1px solid #ddd",
                    borderRadius: "4px",
                    fontSize: "12px",
                    color: "#999",
                  },
                },
                "加载失败"
              ),
          }
        );
      },
    },
    {
      label: "用户昵称",
      prop: "username",
    },
    { label: "用户登录次数", prop: "times" },
    { label: "微信OpenID", prop: "open_id", renderProps: { multiple: false } },
    {
      label: "用户状态",
      prop: "status",
      cellRender: ({ row }) => {
        const statusMap = {
          1: { text: "正常", type: "success" },
          2: { text: "禁用", type: "warning" },
        };
        const status = statusMap[row.status] || {
          text: "未知",
          type: "info",
        };
        return h(ElTag, { type: status.type }, () => status.text);
      },
    },
    {
      label: "注册日期",
      prop: "created_at"
    },
    {
      type: "operation",
      label: () => t("crud.operation"),
      operationConfigure: {
        actions: [
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) => hasAuth("attraction_user:attractionUser:update"),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            show: ({ row }) => hasAuth("attraction_user:attractionUser:delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
